package car.aistar.controller;

import car.aistar.bo.LaoxuUpdate;
import car.aistar.dto.Result;
import car.aistar.entity.User;
import car.aistar.entity.UserDetail;
import car.aistar.service.UserDetailService;
import car.aistar.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
*@Author laoxu
*@Description //TODO 个人中心用户信息修改
*@Date 7:19 2020/10/7
*@Param
*@return
**/
@RestController
@RequestMapping("userDetail")
public class UserDetailController {
    /**
     * 服务对象
     */
    @Autowired
    private UserDetailService userDetailService;

    @Autowired
    private UserService userService;


    @PostMapping("update")
    public @ResponseBody
    Result updateDetail(LaoxuUpdate laoxuUpdate, HttpServletRequest request){
        //初始化详情实体类对象
        UserDetail userDetail = new UserDetail();
        //用户id
//        String uid = "4";
        Integer d = (Integer) request.getSession().getAttribute("uid");

        userDetail.setUId(Integer.valueOf(d));
        userDetail.setId(Long.valueOf(d));
        userDetail.setUname(laoxuUpdate.getUname());
        userDetail.setUpwd(laoxuUpdate.getUpwd());
        userDetail.setUphoto(laoxuUpdate.getUphoto());
        UserDetail userDetail1 = userDetailService.update(userDetail);

        //更新到用户表中
        User user = new User();
        user.setUId(d);
        user.setUname(laoxuUpdate.getUname());
        user.setPwd(laoxuUpdate.getUpwd());
        user.setPhone(laoxuUpdate.getUphoto());
        User user1 = userService.update(user);

        return new Result("200","修改成功",userDetail1);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public UserDetail selectOne(Long id) {
        return this.userDetailService.queryById(id);
    }

}